//
//  CreateQuanziView.m
//  YiqiMobile
//
//  Created by henry on 13-4-30.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "CreateQuanziView.h"
#import "QuartzCore/QuartzCore.h"
#import "Config.h"
#import "Utility.h"
#include "DefineAction.h"
#import "AppDelegate.h"
#import "JSON.h"


@implementation CreateQuanziView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         self.backgroundColor=[UIColor clearColor];
        UIView *createview=[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 150)];
        createview.layer.cornerRadius=10;
        createview.layer.masksToBounds=YES;
        createview.backgroundColor=[UIColor whiteColor];
        [self addSubview:createview];
        
        UILabel *quanziLabel=[Utility detailTextLabel:CGRectMake(12, 13, 100, 13) andFontSize:12];
        quanziLabel.text=@"输入圈子名称";
        [createview addSubview:quanziLabel];
        
        UIImageView *inputBg=[[UIImageView alloc] initWithFrame:CGRectMake(11.5, 36, 257, 54)];
        inputBg.image=[Utility imageNamed:@"createQuanzi_inputbg.png"];
        [createview addSubview:inputBg];
        
        
        self.textView=[[UITextView alloc] initWithFrame:CGRectMake(11.5, 36, 257, 54)];
        self.textView.backgroundColor=[UIColor  clearColor];
        self.textView.layer.cornerRadius = 8;//设置那个圆角的有多圆
        self.textView.layer.borderWidth = 0;//设置边框的宽度，当然可以不要
        //self.textView.layer.borderColor = [[UIColor blackColor] CGColor];//设置边框的颜色
        self.textView.keyboardType=UIKeyboardTypeDefault;
        self.textView.layer.masksToBounds = YES;//设为NO去试试
        self.textView.autocorrectionType=UITextAutocorrectionTypeNo;
        self.textView.returnKeyType=UIReturnKeySend;
        self.textView.textColor=[UIColor blackColor];
        self.textView.font=[UIFont fontWithName:FONT_FAMILY size:15.0];
        self.textView.enablesReturnKeyAutomatically=YES;
        self.textView.delegate=self;
        [createview addSubview:self.textView];
        //[self.textView  becomeFirstResponder];
        [createview addSubview:[Utility customButton:self action:@selector(saveAction:) normalImage:@"quanzi_cannel_normal.png" lightedImage:@"quanzi_cannel_hover.png" frame:CGRectMake(12, 93, 124, 31) title:nil tag:66]];
        [createview addSubview:[Utility customButton:self action:@selector(saveAction:) normalImage:@"quanzi_save_normal.png" lightedImage:@"quanzi_save_hover.png" frame:CGRectMake(144, 93, 124, 31) title:nil tag:67]];

        // Initialization code
    }
    return self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (1 == range.length) {//按下回格键
        return YES;
    }
    if ([text isEqualToString:@"\n"]) {//按下return键
        [self saveAction:nil];
        //这里隐藏键盘，不做任何处理
        //[textView resignFirstResponder];
        
        return NO;
    }else {
        if ([textView.text length] < 140) {//判断字符个数
            return YES;
        }
    }
    return NO;
}


-(void) saveAction:(UIButton*) sender{
    if (sender.tag==66) {//取消
        [self removeFromSuperview];
        return;
    }
    //保存数据
    NSString *content=self.textView.text;
    NSString *userId=[[NSUserDefaults standardUserDefaults] objectForKey:USERID];
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    if (content==nil||[content isEqualToString:@""]) {
        [Utility customAlertWithMsg:@"请输入内容" andTitle:ALRRTTITLE andDelagate:nil];
        return;
	}
        NSString *url=[NSString stringWithFormat:@"%@/%@?type=0",[Utility  getServerUrl],CREATEQUANZICTION];
        NSMutableDictionary *param=[NSMutableDictionary dictionaryWithObjectsAndKeys:content,@"e.e1",userId,@"e.u0",nil];
        self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
        [self.request startLoadData];

}
- (void)requestFinished:(id)context{
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result objectForKey:@"code"] integerValue]==0) {
            
            if ([[result objectForKey:@"method"] isEqualToString:CREATEQUANZICTION]) {
                NSLog(@"%@",@"createE");
            }else if ([[result objectForKey:@"method"] isEqualToString:CREATEBIAOTIACTION]) {
                NSLog(@"%@",@"createB");
            }
            AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
            CustomTabBarController *nav=[app.tabBarController.viewControllers objectAtIndex:0];
            UIViewController *viewController=[nav.viewControllers  objectAtIndex:0];
            if ([viewController respondsToSelector:@selector(loadPushDataFromServer)]) {
                [viewController performSelector:@selector(loadPushDataFromServer) withObject:nil];
            }
            [self removeFromSuperview];
            
            //[Utility customAlertWithMsg:@"保存成功" andTitle:ALRRTTITLE andDelagate:nil];
           
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}


@end
